package com.khson.knows.portal.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.khson.knows.portal.exception.ServiceException;
import com.khson.knows.portal.mapper.ClassroomMapper;
import com.khson.knows.portal.mapper.UserMapper;
import com.khson.knows.portal.mapper.UserRoleMapper;
import com.khson.knows.portal.model.Classroom;
import com.khson.knows.portal.model.User;
import com.khson.knows.portal.model.UserRole;
import com.khson.knows.portal.service.IUserService;
import com.khson.knows.portal.vo.RegisterVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author com.khson
 * @since 2021-08-23
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private ClassroomMapper classroomMapper;
    @Autowired
    private UserRoleMapper userRoleMapper;
    @Override
    public void registerStudent(RegisterVo registerVo) {
        //1.根据邀请码查询班级
        QueryWrapper<Classroom> wrapper = new QueryWrapper<>();
        wrapper.eq("invite_code",registerVo.getInviteCode());
        Classroom classroom = classroomMapper.selectOne(wrapper);
        //2.班级不存在抛出异常
        if(classroom==null){
            throw new ServiceException("邀请码错误!");
        }
        //3.根据用户输入的手机查询用户信息
        User user = userMapper.findUserByUsername(registerVo.getPhone());
        //4.用户已存在抛出异常
        if(user!=null){
            throw new ServiceException("手机号已经被注册");
        }
        //5.密码加密
        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        String pwd = "{bcrypt}"+encoder.encode(registerVo.getPassword());
        //6.实例化User对象进行相关属性的赋值
        User u = new User();
        u.setUsername(registerVo.getPhone());
        u.setPassword(pwd);
        u.setNickname(registerVo.getNickname());
        u.setClassroomId(classroom.getId());
        u.setCreatetime(LocalDateTime.now());
        u.setEnabled(1);//可用
        u.setLocked(0);//不锁定
        u.setType(0);//学生
        //7.将用户新增到数据库
        int num = userMapper.insert(u);
        //num 是数据操作影响的行数,新增一般是1,如果返回0表示新增失败
        if(num==0) {
            throw new ServiceException("数据库异常");
        }
        //8.新增用户和角色的关系
        UserRole userRole = new UserRole();
        userRole.setUserId(u.getId());
        userRole.setRoleId(2);
        num = userRoleMapper.insert(userRole);
        if(num==0) {
            throw new ServiceException("数据库异常");
        }
    }

    private List<User> teachers = new CopyOnWriteArrayList<>();
    private Map<String,User> teacherMap = new ConcurrentHashMap<>();
    @Override
    public List<User> findTeachers() {
        if(teachers.isEmpty()) {
            synchronized (teachers) {
                if(teachers.isEmpty()) {
                    QueryWrapper<User> wrapper = new QueryWrapper<>();
                    wrapper.eq("type", 1);
                    List<User> list = userMapper.selectList(wrapper);
                    teachers.addAll(list);
                    for(User u:list){
                        teacherMap.put(u.getNickname(),u);
                    }
                }
            }
        }
        return teachers;
    }

    @Override
    public Map<String, User> getTeacherMap() {
        if(teacherMap.isEmpty()) {
            findTeachers();
        }
        return teacherMap;
    }
}
